package cn.jbolt.ec.wx;

import cn.hutool.http.HttpUtil;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.ArrayList;
import java.util.List;

public class MsgService {
    /**
     *
     * @param content
     * @param wxMessage
     * @return
     *
     */
    public static final MsgService me=new MsgService();
    WxMpService wxMpService = GenService.getWxMpService("jmd");

    public WxMpXmlOutMessage text_msg(String content, WxMpXmlMessage wxMessage) {
        WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
                .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
                .build();
        return m;
    }

    /**
     * 模板消息

     */
    public boolean sendMsg(String temp_ids, String openid, String first, String keyword1, String keyword2, String keyword3, String keyword4, String keyword5
            , String remark){
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(temp_ids);
        templateMessage.setToUser(openid);
        templateMessage.setUrl("https://ecb.foxcodes.cn/shop/index.html");

        List<WxMpTemplateData> datas = new ArrayList<>();
        if (null==first)
            return false;
        WxMpTemplateData first1 = new WxMpTemplateData();
        first1.setName("first");
        first1.setValue(first);
        datas.add(first1);
        if (null!=keyword1){
            WxMpTemplateData word1 = new WxMpTemplateData();
            word1.setName("keyword1");
            word1.setValue(keyword1);
            datas.add(word1);
        }
        if (null!=keyword2){
            WxMpTemplateData word2 = new WxMpTemplateData();
            word2.setName("keyword2");
            word2.setValue(keyword2);
            datas.add(word2);
        }
        if (null!=keyword3){
            WxMpTemplateData word3 = new WxMpTemplateData();
            word3.setName("keyword3");
            word3.setValue(keyword3);
            datas.add(word3);
        }
        if (null!=keyword4){
            WxMpTemplateData word4 = new WxMpTemplateData();
            word4.setName("keyword4");
            word4.setValue(keyword4);
            datas.add(word4);
        }
        if (null!=keyword5){
            WxMpTemplateData word5 = new WxMpTemplateData();
            word5.setName("keyword5");
            word5.setValue(keyword5);
            datas.add(word5);
        }
        if (null!=remark){
            WxMpTemplateData remark1 = new WxMpTemplateData();
            remark1.setName("remark");
            remark1.setValue(remark);
            datas.add(remark1);
        }

        templateMessage.setData(datas);
//        System.out.println("temp msg in");
//        System.out.println(templateMessage.getData());
        try{
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

        }catch (Exception e){
            System.out.println(e.getMessage());

        }
        return false;
    }

    public void message_custom_send(String openid){
        HttpUtil.post(
                "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="
                        + SubscribeService.me.getAccessToken(), shopJson(openid,"商城首页","嘉美达商城"
                        ,"https://ecb.foxcodes.cn/shop/index.html","http://puo80pzgp.bkt.clouddn.com/ShopFirst.jpg"));
    }

    /**
     * 图文信息json格式
     * @param openid
     * @return
     */
    public String shopJson(String openid,String title,String description,String url,String piurl){

        final String paramjson = "{\n" +
                "    \"touser\": \"" + "%s" + "\", \n" +
                "    \"msgtype\": \"news\", \n" +
                "    \"news\": {\n" +
                "        \"articles\": [\n" +
                "            {\n" +
                "                \"title\": \""+ "%s" +"\", \n" +
                "                \"description\": \""+ "%s" +"\", \n" +
                "                \"url\": \""+ "%s" +"\", \n" +
                "                \"picurl\": \""+ "%s" +"\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";  //图文消息链接
        return String.format(paramjson,openid,title,description,url,piurl);
    }

    public String spreader_json(String openid,String url,String name){
        String paramjson = "{\n" +
                "    \"touser\": \"" + "%s" + "\", \n" +
                "    \"msgtype\": \"news\", \n" +
                "    \"news\": {\n" +
                "        \"articles\": [\n" +
                "            {\n" +
                "                \"title\": \"嘉美达分销\", \n" +
                "                \"description\": \""+"%s"+"邀请您加入嘉美达\", \n" +
                "                \"url\": \""+"%s"+"\", \n" +
                "                \"picurl\": \""+"%s"+"\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";  //图文消息链接
        return String.format(paramjson,openid,name,url,url);
    }

}
